package com.xiaoyu.lanling.feature.gift.fragment

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.view.list.GridSpacingItemDecoration
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.gift.GiftListEvent
import com.xiaoyu.lanling.event.gift.GiftSelectEvent
import com.xiaoyu.lanling.feature.gift.model.Gift
import com.xiaoyu.lanling.feature.gift.viewholder.GiftViewHolder
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.gift_normal_list_fragment.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

class NormalGiftListFragment : AbstractGiftListFragment<Gift>() {

    companion object {
        fun create(gifts: ArrayList<Gift>): OnShowFragment {
            val params = Bundle()
            params.putSerializable(Constant.IntentKey.KEY_GIFT_LIST, gifts)

            val fragment: OnShowFragment = NormalGiftListFragment()
            fragment.arguments = params
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.gift_normal_list_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initAdapter()
        initEvent()
        initData()
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GiftListEvent) {
                update(event.normalGifts)
                selectFirstGift()
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GiftSelectEvent) {
                updateForSelectGift(event.item)
            }
        })
    }

    private fun initData() {
        val gifts = arguments?.getSerializable(Constant.IntentKey.KEY_GIFT_LIST) as? List<Gift>
        update(gifts)
    }

    private fun updateForSelectGift(item: Gift) {
        if (mAdapter.list == null || item !in mAdapter.list) return
        for (giftItem in mAdapter.list) {
            giftItem.isSelected = item == giftItem
        }
        mAdapter.notifyDataSetChanged()
    }

    override fun selectFirstGift() {
        mAdapter.list ?: return
        for (item in mAdapter.list) {
            if (item.canGive) {
                GiftSelectEvent(item).post()
                break
            }
        }
    }

    override fun getLayoutID(): Int {
        return R.layout.gift_normal_list_fragment
    }

    override fun initAdapter() {
        mAdapter = CubeRecyclerViewSimpleAdapter()
        mAdapter.setViewHolderClass(GiftListEvent.NORMAL_ITEM, null, GiftViewHolder::class.java, 12)
        gift_recycler_view.adapter = mAdapter
        gift_recycler_view.addItemDecoration(GridSpacingItemDecoration(4, 8, 8, true))
    }
}